<template>
  <view class="container" style="background-image: url(../../static/login/login.jpg); background-position: center center;  background-repeat: no-repeat;">
    <view class="login-form">
      <view class="title">登录</view>
      <form @submit.prevent="login">
        <view class="form-item">
          <input type="text" v-model="username" placeholder="用户名" required />
        </view>
        <view class="form-item">
          <input type="password" v-model="password" placeholder="密码" required />
        </view>
        <button type="submit" @click="login()">登录</button>
      </form>
    </view>
    <view class="register-link">
      <text>还没有账号？</text>
      <navigator url="/pages/register/register">注册</navigator>
    </view>
  </view>
</template>

<script>
	import config from '@/config/config.js'; // 引入全局配置文件
export default {
  data() {
    return {
      username: '',
      password: ''
    };
  },
  methods: {
    login() {
		if(this.username==''||this.password==''){
			uni.showToast({
				icon:'none',
				title:"请输入完整账号密码"
			})
			return
		}
		let user = {
			username:this.username,
			password:this.password
		}
		uni.request({
		  url: config.baseURL + '/users/login',
		  method: 'POST',
		  data: user,
		  success: (res) => {
		    if (res.data.status == true) {
			//存储用户信息，备用
			uni.setStorageSync('userInfo', res.data.data);
			//登录成功弹窗	
		      uni.showToast({
		        title: '登录成功',
		        icon: 'success',
		        duration: 2000,
		        success: () => {
		          uni.reLaunch({
						url:"../index/index"
					})
		        }
		      });
		    } else {
				uni.showToast({
					icon:'none',
					title:"您的信息有误"
				})
		      console.error('登录失败', res.data.message);
		    }
		  },
		  fail: (err) => {
		    console.error('请求失败', err);
		  }
		});
    }
  }
};
</script>

<style>
.container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f0f0f0;
}

.login-form {
  background-color: #fff;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

.title {
  text-align: center;
  margin-bottom: 20px;
  font-size: 20px;
  font-weight: bold;
}

.form-item {
  margin-bottom: 20px;
}

input {
  width: 89%;
  padding: 10px;
  border: 1px solid #ccc;
  border-radius: 4px;
}

button {
  width: 100%;
  padding: 10px;
  background-color: #007bff;
  color: #fff;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

button:hover {
  background-color: #0056b3;
}

.register-link {
  margin-top: 20px;
  text-align: center;
}

.register-link text {
  margin-right: 5px;
  color: #007bff;
}
</style>
